package com.dailyblue.java.spring.boot.mvc.mybatis.mapper;

import com.dailyblue.java.spring.boot.commons.bean.Post;
import com.dailyblue.java.spring.boot.commons.bean.Reply;
import com.dailyblue.java.spring.boot.commons.bean.User;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Set;

@Mapper
public interface PostMapper {
    @Select("select post.*,nickname from post left join user on post.userid=user.id where post.state=1 and sectionid=#{sid}")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "replyCount",column = "id",javaType = Integer.class,
                    one = @One(select = "com.dailyblue.java.spring.boot.mvc.mybatis.mapper.PostMapper.findReplyCountByPostId"))
    })
    List<Post> findAllPostBySectionId(int sid);
    @Select("select * from post where id=#{id} and state=1")
    @Results({
            @Result(property = "id",column = "id",id = true),
            @Result(property = "user",column = "userId",javaType = User.class,
                    one = @One(select = "com.dailyblue.java.spring.boot.mvc.mybatis.mapper.UserMapper.findUserById")),
            @Result(property = "replySet",column = "id",javaType = List.class,
                    many = @Many(select = "com.dailyblue.java.spring.boot.mvc.mybatis.mapper.ReplyMapper.findAllReplyByPostId"))
    })
    Post findPostById(int id);
    @Select("select count(id) from reply where postId=${id}")
    int findReplyCountByPostId(int id);
    @Insert("insert into post values(null,#{postTitle},#{postContent},now(),#{sectionId},#{userId},1)")
    void savePost(Post post);
}
